﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;

namespace ChinaClipping.ClippingMaster
{
    class TreeNodeHelper
    {
        public static TreeNode GetTreeNodeFromFile(string path)
        {

            byte[] bytes = File.ReadAllBytes(path);
            using (MemoryStream stream = new MemoryStream(bytes))
            {
                var soapFormatter = new SoapFormatter();
                TreeNode tn = soapFormatter.Deserialize(stream) as TreeNode;
                if (tn != null)
                {
                    convertTreeNodeTagType(tn);
                }
                return tn;
            }

        }

        private static void convertTreeNodeTagType(TreeNode treeNode)
        {
            if (treeNode.Tag is ClippingMaster.SearchEngines.SearchObject)
            {
                treeNode.Tag = typeConvert(treeNode.Tag as ClippingMaster.SearchEngines.SearchObject);
               
            }
            foreach (TreeNode tn in treeNode.Nodes)
                convertTreeNodeTagType(tn);
        }
        private static Clipping.Infrastructure.Models.PeriodSearchObject typeConvert(ClippingMaster.SearchEngines.SearchObject searchObj)
        {
            Clipping.Infrastructure.Models.PeriodSearchObject rtn = new Clipping.Infrastructure.Models.PeriodSearchObject(searchObj.Keyword,
                (Clipping.Infrastructure.Models.SearchType)searchObj.SearchType,
                (Clipping.Infrastructure.Models.SearchPeriod)searchObj.SearchTimeSpan);
            return rtn;
        }

    }
}
